package sit.engreal.leetcode.problems200;

import org.junit.jupiter.api.Assertions;

/**
 * <a href="https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/">167. 两数之和 II - 输入有序数组</a>
 */
public class LeetCode167 {
    public static class Solution {
        public int[] twoSum(int[] numbers, int target) {
            int i = 0;
            int j = numbers.length - 1;

            while (i < j) {
                int sum = numbers[i] + numbers[j];
                if (sum < target) {
                    i++;
                } else if (sum > target) {
                    j--;
                } else {
                    return new int[]{i + 1, j + 1};
                }
            }
            return new int[]{-1, -1};
        }
    }

    public static void main(String[] args) {
        final Solution solution = new Solution();

        Assertions.assertArrayEquals(
                new int[]{1, 2},
                solution.twoSum(new int[]{2, 7, 11, 15}, 9)
        );

        Assertions.assertArrayEquals(
                new int[]{1, 3},
                solution.twoSum(new int[]{2, 3, 4}, 6)
        );

        Assertions.assertArrayEquals(
                new int[]{1, 2},
                solution.twoSum(new int[]{-1, 0}, -1)
        );
    }
}
